package com.stylefeng.guns.modular.hhtb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.hhtb.model.Configs;
import com.stylefeng.guns.modular.hhtb.service.IConfigsService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统配置控制器
 *
 * @author fengshuonan
 * @Date 2018-08-29 11:30:00
 */
@Controller
@RequestMapping("/configs")
public class ConfigsController extends BaseController {

    private String PREFIX = "/hhtb/configs/";

    @Autowired
    private IConfigsService configsService;

    /**
     * 跳转到理财设置
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("page",1);
        return PREFIX + "configs.html";
    }
    /**
     * 跳转到基础设置
     */
    @RequestMapping("/basicIntercalate")
    public String basicIntercalate(Model model) {
        model.addAttribute("page",2);
        return PREFIX + "configs2.html";
    }
    /**
     * 跳转到系统设置
     */
    @RequestMapping("/systemSetup")
    public String systemSetup(Model model) {
        model.addAttribute("page",3);
        return PREFIX + "configs3.html";
    }

    /**
     * 跳转到添加系统配置表
     */
    @RequestMapping("/configs_add")
    public String configsAdd() {
        return PREFIX + "configs_add.html";
    }

    /**
     * 跳转到查看系统配置表
     */
    @RequestMapping("/configs_update/{configsId}")
    public String configsUpdate(@PathVariable Integer configsId, Model model) {
        Configs configs = configsService.selectById(configsId);
        model.addAttribute("item",configs);
        LogObjectHolder.me().set(configs);
        return PREFIX + "configs_edit.html";
    }

    /**
     * 跳转到修改系统配置表
     */
    @RequestMapping("/configs_update2/{configsId}")
    public String configsUpdate2(@PathVariable Integer configsId, Model model) {
        Configs configs = configsService.selectById(configsId);
        model.addAttribute("item",configs);
        LogObjectHolder.me().set(configs);
        return PREFIX + "configs_edit2.html";
    }

    /**
     * 获取理财设置基础设置列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Configs configs) {
       List<Configs> list = configsService.selectList(new EntityWrapper<Configs>().eq("page", configs.getPage()));
        return list;
    }
    /**
     * 获取系统配置表列表
     */
    @RequestMapping(value = "/systemList")
    @ResponseBody
    public Object systemList(Configs configs) {
        Page<Map<String,Object>> pages = new PageFactory<Map<String,Object>>().defaultPage();
        List<Map<String, Object>>articlesList= configsService.configsAllist(pages,configs);
        pages.setRecords(articlesList);
        return super.packForBT(pages);
    }

    /**
     * 新增系统配置表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Configs configs) {
        if(configs.getFieldValue() != null && !"".equals(configs.getFieldValue())){
            configs.setFieldValue(org.springframework.web.util.HtmlUtils.htmlUnescape(configs.getFieldValue()));
        }
        Map<String,String> result = new HashMap<String,String>();
        String name = configs.getFieldName();
        //Configs fieldName = configsService.selectOne(new EntityWrapper<Configs>().eq("fieldName", configs.getFieldName()));
        Configs fieldName = configsService.selectOne(new EntityWrapper<Configs>().eq("fieldName", name));
        if (null ==fieldName){
            configs.setType(3);
            configs.setPage(3);
            configsService.insert(configs);
            result.put("SUCCESS","添加成功");
            return result;
        }
        result.put("ERROR","当前字段名称已存在!");
        return result;
    }

    /**
     * 删除系统配置表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer configsId) {
        configsService.deleteById(configsId);
        return SUCCESS_TIP;
    }

    /**
     * 修改系统配置表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Configs configs) {
        if(configs.getFieldValue() != null && !"".equals(configs.getFieldValue())){
            configs.setFieldValue(org.springframework.web.util.HtmlUtils.htmlUnescape(configs.getFieldValue()));
        }
        configsService.updateById(configs);
        return SUCCESS_TIP;
    }

    /**
     * 修改基础设置
     */
    @RequestMapping(value = "/updateJc")
    @ResponseBody
    public Object updateJc(@RequestBody List<Configs> configs) {
        configsService.updateByIds(configs);
        return SUCCESS_TIP;
    }

    /**
     * 系统配置表详情
     */
    @RequestMapping(value = "/detail/{configsId}")
    @ResponseBody
    public Object detail(@PathVariable("configsId") Integer configsId) {
        return configsService.selectById(configsId);
    }

    /**
     * 图片上传阿里云
     *
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/aliyunUpload")
    @ResponseBody
    public Map<String, Object> upload(HttpServletRequest request){
        return configsService.upload(request);
    }

}
